package com.guang.springbootselfmvc.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.guang.springbootselfmvc.controlleradvice.ExceptionControllerAdvice;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Iterator;
import java.util.List;

/**
 *
 * @EnableWebMvc必须配合@Configuration注解一起使用
 *
 *  Adding this annotation to an @Configuration class imports the Spring MVC configuration from WebMvcConfigurationSupport
 *
 * @EnableWebMvc imports DelegatingWebMvcConfiguration, which:
 * Provides default Spring configuration for Spring MVC applications
 * Detects and delegates to WebMvcConfigurer implementations to customize that configuration.
 *
 * 我们这样子来进行操作之后，WebMvcAutoConfiguration这个就不会再来进行生效了。因为DelegatingWebMvcConfiguration继承WebMvcConfigurationSupport
 *
 * 所以WebMVC的配置将不会再生效了。原来可以看到WebMvcAutoConfiguration上标注了@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
 * 所以默认配置的将会来失效
 *
 * @author liguang
 * @date 2022/5/23 9:55
 */
//@Configuration
//// 这个注解能够导入一个配置类，可以将一些mvc中一些默认的组件配置到容器中来
//// @EnableWebMvc
//public class CustomWebMVCConfig {
//
//}

@Configuration
public class WebConfig implements WebMvcConfigurer {


    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        Iterator var2 = converters.iterator();

        while(var2.hasNext()) {
            HttpMessageConverter<?> converter = (HttpMessageConverter)var2.next();
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                ObjectMapper objectMapper = ((MappingJackson2HttpMessageConverter)converter).getObjectMapper();
                objectMapper.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
            }
        }

    }

    @Bean
    public ExceptionControllerAdvice exceptionControllerAdvice() {
        return new ExceptionControllerAdvice();
    }
}
